package com.dianrui.fmi.community;

import android.support.design.widget.BottomSheetDialog;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dianrui.fmi.Constant;
import com.dianrui.fmi.R;
import com.dianrui.fmi.activity.BaseActivity;
import com.dianrui.fmi.adapter.CommentInfoListAdapter;
import com.dianrui.fmi.bean.CommentSubClass;
import com.dianrui.fmi.event.RfreshAnswer;
import com.dianrui.fmi.event.RfreshCommunity;
import com.dianrui.fmi.event.RfreshFavorites;
import com.dianrui.fmi.event.RfreshQuestion;
import com.dianrui.fmi.util.GlideUtil;
import com.dianrui.fmi.util.MyUtil;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.ToastUtil;
import com.dianrui.fmi.util.Url;
import com.dianrui.fmi.view.pulltoRefresh.PullToRefreshBase;
import com.dianrui.fmi.view.pulltoRefresh.PullToRefreshListView;
import com.google.gson.JsonObject;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 *
 */

public class CommentInfoActivity extends BaseActivity {
    private String commentId;
    private int mPageIndex = 1;
    private CommentInfoListAdapter commentunityInfoListAdapter;
    private List<CommentSubClass> commentInfoLists = new ArrayList<>();
    private String communityId;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.face)
    ImageView face;
    @BindView(R.id.nickname)
    TextView nickname;
    @BindView(R.id.create_time)
    TextView createTime;
    @BindView(R.id.contents)
    TextView contents;
    @BindView(R.id.community_data_layout)
    LinearLayout communityDataLayout;
    @BindView(R.id.community_ino_nodata)
    LinearLayout communityInoNodata;
    @BindView(R.id.community_info_list)
    PullToRefreshListView listView;



    @Override
    public int getContentViewId() {
        return R.layout.activity_comment_info;
    }

    @Override
    public void initView() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            commentId = getIntent().getStringExtra("commentId");
        }
        title.setText(getString(R.string.comment_info_title));
        commentunityInfoListAdapter = new CommentInfoListAdapter(CommentInfoActivity.this, commentInfoLists);
        listView.setAdapter(commentunityInfoListAdapter);
        getSubCommentList(false);
        listView.setMode(PullToRefreshBase.Mode.BOTH);
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                getSubCommentList(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mPageIndex++;
                getSubCommentList(false);
            }
        });


    }

    private void getSubCommentList(final boolean isRfresh) {
        if (isRfresh) {
            mPageIndex = 1;
        }
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("comment_id", commentId);
        jsonObject.addProperty("p", mPageIndex);
        OkHttpUtils.getInstance().postWithMemberToken(Url.SUBCLASS, jsonObject.toString(), new OkHttpUtils.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (listView != null) {
                    listView.onRefreshComplete();
                }
                if (isRfresh) {
                    commentInfoLists.clear();
                }
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                GlideUtil.loadUserImageViewSize(getApplicationContext(), jsonObject.optJSONObject("data").optString("face"), face);
                                communityId = jsonObject.optJSONObject("data").optString("community_id");
                                nickname.setText(jsonObject.optJSONObject("data").optString("nickname"));
                                createTime.setText(jsonObject.optJSONObject("data").optString("create_time"));
                                contents.setText(jsonObject.optJSONObject("data").optString("contents"));
                                JSONArray jsonArray1 = jsonObject.optJSONObject("data").optJSONArray("subclass");
                                for (int j = 0; j < jsonArray1.length(); j++) {
                                    CommentSubClass commentSubClass = new CommentSubClass();
                                    commentSubClass.comment_id = jsonArray1.optJSONObject(j).optString("comment_id");
                                    commentSubClass.contents = jsonArray1.optJSONObject(j).optString("contents");
                                    commentSubClass.create_time = jsonArray1.optJSONObject(j).optString("create_time");
                                    commentSubClass.community_id = jsonArray1.optJSONObject(j).optString("community_id");
                                    commentSubClass.member_id = jsonArray1.optJSONObject(j).optString("member_id");
                                    commentSubClass.nickname = jsonArray1.optJSONObject(j).optString("nickname");
                                    commentSubClass.face = jsonArray1.optJSONObject(j).optString("face");
                                    commentInfoLists.add(commentSubClass);
                                }
                                if (commentunityInfoListAdapter != null) {
                                    commentunityInfoListAdapter.refresh(commentInfoLists);
                                }

                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

    }



    @OnClick({R.id.back, R.id.reply_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.reply_btn:
                final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(CommentInfoActivity.this, R.style.BottomSheetStyle);
                View view1 = LayoutInflater.from(CommentInfoActivity.this).inflate(R.layout.dialog_input_reply, null);
                final EditText input = view1.findViewById(R.id.reply_content);
                final Button yes_dialog = view1.findViewById(R.id.reply_btn);
                final Button cancel_dialog = view1.findViewById(R.id.cancel_btn);
                input.setMovementMethod(new ScrollingMovementMethod());
                yes_dialog.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (StringUtils.isEmpty(input.getText().toString())) {
                            ToastUtil.showToast(getString(R.string.please_input_reply_content));
                        } else {
                            bottomSheetDialog.dismiss();
                            ReplyCommentPost(input, communityId);
                        }
                    }
                });
                cancel_dialog.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomSheetDialog.dismiss();
                    }
                });
                bottomSheetDialog.setContentView(view1);
                bottomSheetDialog.setCanceledOnTouchOutside(true);
                if (!MyUtil.getActivity(bottomSheetDialog.getContext()).isFinishing()) {
                    bottomSheetDialog.show();
                }
                break;
        }
    }


    private void ReplyCommentPost(EditText input, String community_id) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("community_id", community_id);
        jsonObject.addProperty("contents", input.getText().toString().trim());
        OkHttpUtils.getInstance().postWithMemberToken(Url.INSERTCOMMON, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                ToastUtil.showToast(jsonObject.optString("message"));
                                EventBus.getDefault().post(new RfreshFavorites());
                                EventBus.getDefault().post(new RfreshCommunity());
                                EventBus.getDefault().post(new RfreshQuestion());
                                EventBus.getDefault().post(new RfreshAnswer());
                            } else {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            }
        });
    }

}
